package com.atwulidun.linkedlist.testSingleLinkedList;

public class SingleLinkedListDemo {
    public static void main(String[] args) {
        HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode hero2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode hero3 = new HeroNode(3, "吴用", "智多星");
        HeroNode hero4 = new HeroNode(4, "林冲", "豹子头");
        HeroNode hero5 = new HeroNode(5, "秦明", "霹雳火");
        HeroNode hero6 = new HeroNode(6, "柴进", "小旋风");

        SingleLinkedList list1 = new SingleLinkedList();
        SingleLinkedList list2 = new SingleLinkedList();
        list1.addByOrder(hero1);
        list1.addByOrder(hero3);
        list1.addByOrder(hero5);
        list2.addByOrder(hero2);
        list2.addByOrder(hero4);
        list2.addByOrder(hero6);
        HeroNode node = list1.recursiveMerge(list1.getHead().next, list2.getHead().next);
        list1.listByHeadNode(node);
    }
}

class SingleLinkedList {
    private HeroNode head = new HeroNode(Integer.MAX_VALUE, null, null);

    public HeroNode getHead() {
        return head;
    }

    // 不按照序号添加
    public void add(HeroNode heroNode) {
        HeroNode temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = heroNode;
    }

    // 按照序号添加
    public void addByOrder(HeroNode heroNode) {
        HeroNode temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no > heroNode.no) {
                break;
            }
            temp = temp.next;
        }
        heroNode.next = temp.next;
        temp.next = heroNode;
    }

    // 按照序号修改
    public void update(HeroNode heroNode) {
        HeroNode temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (heroNode.no > temp.next.no) {
                temp = temp.next;
            } else if (heroNode.no == temp.next.no) {
                flag = true;
                break;
            } else {
                break;
            }
        }
        if (!flag) {
            System.out.println("要修改的节点不存在！");
        } else {
            temp.next.name = heroNode.name;
            temp.next.nickname = heroNode.nickname;
        }
    }

    // 根据编号来删除节点
    public void delete(int no) {
        HeroNode temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (no > temp.next.no) {
                temp = temp.next;
            } else if (no == temp.next.no) {
                flag = true;
                break;
            } else {
                break;
            }
        }
        if (!flag) {
            System.out.println("要删除的节点不存在！");
        } else {
            temp.next = temp.next.next;
        }
    }

    // 显示链表信息
    public void list() {
        HeroNode temp = head;
        while (temp.next != null) {
            System.out.println("no=" + temp.next.no + ",name=" + temp.next.name + ",nickName=" + temp.next.nickname);
            temp = temp.next;
        }
    }

    // 根据头结点显示链表信息
    public void listByHeadNode(HeroNode head) {
        if (head == null) {
            return;
        }
        HeroNode temp = head;
        while (temp != null) {
            System.out.println("no=" + temp.no + ",name=" + temp.name + ",nickName=" + temp.nickname);
            temp = temp.next;
        }
    }

    // 获取单链表节点的个数，若带头结点，则不考虑头结点
    public int getLength(HeroNode head) {
        int length = 0;
        HeroNode temp = head.next;
        while (temp != null) {
            length++;
            temp = temp.next;
        }
        return length;
    }

    // 查找单链表中的倒数第 k 个节点
    public HeroNode getLastKthHeroNode(HeroNode head, int k) {
        int length = getLength(head);
        if (length == 0 || k < 0 || k > length) {
            throw new IllegalArgumentException("输入参数不合法！");
        }
        HeroNode temp = head;
        for (int i = 0; i < length - k; i++) {
            temp = temp.next;
        }
        return temp.next;
    }

    // 单链表的反转
    public HeroNode reverseLinkedList(HeroNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        HeroNode dummyNode = new HeroNode(Integer.MAX_VALUE, null, null);
        HeroNode curNode = head;
        HeroNode nextNode;
        while (curNode != null) {
            nextNode = curNode.next;
            curNode.next = dummyNode.next;
            dummyNode.next = curNode;
            curNode = nextNode;
        }
        return dummyNode.next;
    }

    // 从尾到头打印链表
    public void reversePrint(HeroNode head) {
        if (head == null) {
            return;
        }
        reversePrint(head.next);
        System.out.println("no=" + head.no + ",name=" + head.name + ",nickName=" + head.nickname);
    }

    // 合并两个有序单链表(迭代)
    public HeroNode iterativeMerge(HeroNode head1, HeroNode head2) {
        if (head1 == null) {
            return head2;
        }
        if (head2 == null) {
            return head1;
        }
        HeroNode dummyNode = new HeroNode(Integer.MAX_VALUE, null, null);
        HeroNode temp = dummyNode;
        while (head1 != null && head2 != null) {
            if (head1.no < head2.no) {
                temp.next = head1;
                temp = temp.next;
                head1 = head1.next;
            } else {
                temp.next = head2;
                temp = temp.next;
                head2 = head2.next;
            }
        }
        if (head1 == null) {
            temp.next = head2;
        } else {
            temp.next = head1;
        }
        return dummyNode.next;
    }

    // 合并两个有序单链表(递归)
    public HeroNode recursiveMerge(HeroNode head1, HeroNode head2) {
        if (head1 == null) {
            return head2;
        }
        if (head2 == null) {
            return head1;
        }
        HeroNode temp;
        if (head1.no < head2.no) {
            temp = head1;
            temp.next = recursiveMerge(head1.next, head2);
        } else {
            temp = head2;
            temp.next = recursiveMerge(head1, head2.next);
        }
        return temp;
    }
}

class HeroNode {
    public int no;
    public String name;
    public String nickname;
    public HeroNode next;

    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}